﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Xml.Linq;

namespace WpfXamlWriterReader
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string path = "Sample.xaml";

        public MainWindow()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Create the Button.
            Button originalButton = new Button();
            originalButton.Height = 50;
            originalButton.Width = 100;
            originalButton.Background = Brushes.AliceBlue;
            originalButton.Content = "Click Me";

            // Save the Button to a string.
            string savedButton = XamlWriter.Save(originalButton);
            Trace.Write(savedButton);
            // Load the button
            StringReader stringReader = new StringReader(savedButton);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            Button readerLoadButton = (Button)XamlReader.Load(xmlReader);
  
        }

        Random rand = new Random();

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Create the Button.
            Button originalButton = new Button();
            originalButton.Height = 50;
            originalButton.Width = 100;
            originalButton.Background = Brushes.AliceBlue;
            originalButton.Content = "Click Me";

            Canvas.SetLeft(originalButton, rand.Next(500));
            Canvas.SetTop(originalButton, rand.Next(300));
            canMain.Children.Add(originalButton);
        }

        private void btnResult_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("ff");
            var xdoc = XElement.Parse(XamlWriter.Save(canMain));
            Debug.WriteLine(xdoc.ToString());
            File.WriteAllText(path, xdoc.ToString());
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            if (!File.Exists(path)) return;
            LoadXaml();
            ParseXaml();
        }

        void loadXamlAsyc()
        {
            FileStream ms = new FileStream(path, FileMode.Open);
            XamlReader reader = new XamlReader();
            reader.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(reader_LoadCompleted);
            var t = reader.LoadAsync(ms);
        }

        void reader_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //
        }

        void ParseXaml()
        {
            string txt = File.ReadAllText(path);
            var canvas = XamlReader.Parse(txt);
        }

        void LoadXaml()
        {

            MemoryStream ms = new MemoryStream(File.ReadAllBytes(path));
            var canvas = (Canvas)XamlReader.Load(ms);
            var collection = canvas.Children.OfType<UIElement>().ToList();
            foreach (UIElement element in collection)
            {
                canvas.Children.Remove(element);
                canMain.Children.Add(element);
            }
            canvas = null;
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            canMain.Children.Clear();
        }
    }
}
